package com.lb.jwitter.action.report;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXADMTrangThaiHocVien;
import com.lb.jwitter.service.TblQLDTTXADMTrangThaiHocVienService;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienPhieuDangKyMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienPhieuDangKyMonHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;
import com.lb.jwitter.service.TblQLDTTXAHocVienTaiKhoan;
import com.lb.jwitter.service.TblQLDTTXAHocVienTaiKhoanService;
import com.lb.jwitter.service.TblQLDTTXAHocVienTamDungHocKy;
import com.lb.jwitter.service.TblQLDTTXAHocVienTamDungHocKyService;
import com.lb.jwitter.service.TblQLDTTXAQuiDinhHocPhi;
import com.lb.jwitter.service.TblQLDTTXAQuiDinhHocPhiService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAHocVienXetTamDungReportAction{
    private Message msg; 
    public Message getMsg() { return msg; }

    private Long hocKyId;
    
   public Long getHocKyId() {
		return hocKyId;
	}
	public void setHocKyId(Long hocKyId) {
		this.hocKyId = hocKyId;
	}
   
    
    
    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams;

	public List<TblQLDTTXAHocKyTrongNam> getTblQLDTTXAHocKyTrongNams() {
		return tblQLDTTXAHocKyTrongNams;
	}

	private List<TblQLDTTXAHocVien> tblQLDTTXAHocViens;
    
	public List<TblQLDTTXAHocVien> getTblQLDTTXAHocViens() {
		return tblQLDTTXAHocViens;
	}
	public void setTblQLDTTXAHocViens(List<TblQLDTTXAHocVien> tblQLDTTXAHocViens) {
		this.tblQLDTTXAHocViens = tblQLDTTXAHocViens;
	}

	private List<Double> noHocViens;
	
	public List<Double> getNoHocViens() {
		return noHocViens;
	}

	private TblQLDTTXAHocVienService tblQLDTTXAHocVienService;
    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService;
    private TblQLDTTXAHocVienPhieuDangKyMonHocService tblQLDTTXAHocVienPhieuDangKyMonHocService;
    private TblQLDTTXAHocVienTamDungHocKyService tblQLDTTXAHocVienTamDungHocKyService;
    private List<TblQLDTTXAHocVienTamDungHocKy> tblQLDTTXAHocVienTamDungHocKys;
    

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String form() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_PhieuDangKyMonHoc", "xem")){ 
        return "noprivilege"; 
    } 
 
    tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 
        


        msg = new Message(); 
        List<ConditionReport> cons = new ArrayList<ConditionReport>();
        
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("id");
            con.setValue1("desc");
            con.setOperator("order");
            cons.add(con);	
        } 
        
       tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 0, 0);
        
        
        
        return Action.SUCCESS;
	 }catch(Exception e){ 
	     System.out.println("Loi Report tblQLDTTXAHocVienKhongDangKy2HKLienTiep Action"); 
	     e.printStackTrace(); 
	     return Action.ERROR; 
	}
}
 

	private TblQLDTTXAHocKyTrongNam getHocKyTrongNamBatDauHoc(TblQLDTTXAHocVien hocVien) {
		try {
		String khoaHoc = hocVien.getKhoaHocId().getTen().trim();
		String namBatDauHoc = khoaHoc.substring(2, 4);
		String hocKyTrongNamBatDauHoc = khoaHoc.substring(4);
		for (TblQLDTTXAHocKyTrongNam hk : tblQLDTTXAHocKyTrongNams) {
			String nam = hk.getNamHocId().getTen();
					
			if (nam.substring(nam.length() - 2).equalsIgnoreCase(namBatDauHoc)
					&& hk.getTen().trim().equalsIgnoreCase(hocKyTrongNamBatDauHoc)) {
				return hk;
			}
		}
		return null;
		} catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	private boolean isTamDungHocKy(TblQLDTTXAHocVien hocVien, TblQLDTTXAHocKyTrongNam hocKy){
		try{
			for(TblQLDTTXAHocVienTamDungHocKy thongTinTamDung : tblQLDTTXAHocVienTamDungHocKys){
				if(thongTinTamDung.getHocVienId().getId().equals(hocVien.getId()) && thongTinTamDung.getHocKyId().getId().equals(hocKy.getId())){
					return true;
				}
			}
			return false;
		} catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
 
	private TblQLDTTXAHocKyTrongNam hocKy;
    public TblQLDTTXAHocKyTrongNam getHocKy() {
		return hocKy;
	}
	public void setHocKy(TblQLDTTXAHocKyTrongNam hocKy) {
		this.hocKy = hocKy;
	}
	
	private TblQLDTTXAQuiDinhHocPhiService tblQLDTTXAQuiDinhHocPhiService;
	private TblQLDTTXAHocVienHocPhiMonHocService tblQLDTTXAHocVienHocPhiMonHocService;
	private TblQLDTTXAHocVienTaiKhoanService tblQLDTTXAHocVienTaiKhoanService;
    
	
 public String execute() throws Exception 
 { 
  try{ 	
      Map session = ActionContext.getContext().getSession();
      taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
      if( taiKhoanDangNhap == null) return "noprivilege";
     ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
     tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
     if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_PhieuDangKyMonHoc", "xem")){ 
         return "noprivilege"; 
     } 
     	tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient");
     	tblQLDTTXAHocVienPhieuDangKyMonHocService = (TblQLDTTXAHocVienPhieuDangKyMonHocService) context.getBean("tbl_QLDTTXA_HocVien_PhieuDangKyMonHocClient"); 
     	 
        tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient");
        
        tblQLDTTXAHocVienTamDungHocKyService = (TblQLDTTXAHocVienTamDungHocKyService) context.getBean("tbl_QLDTTXA_HocVien_TamDung_HocKyClient");
        
        List<ConditionReport> cons = new ArrayList<ConditionReport>();
        
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("id");
            con.setValue1("desc");
            con.setOperator("order");
            cons.add(con);	
        } 
        
       tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 0, 0);
        
        tblQLDTTXAQuiDinhHocPhiService = (TblQLDTTXAQuiDinhHocPhiService) context.getBean("tbl_QLDTTXA_QuiDinhHocPhiClient"); 
        
        tblQLDTTXAHocVienTaiKhoanService = (TblQLDTTXAHocVienTaiKhoanService) context.getBean("tbl_QLDTTXA_HocVien_TaiKhoanClient"); 
        
        tblQLDTTXAHocVienHocPhiMonHocService = (TblQLDTTXAHocVienHocPhiMonHocService) context.getBean("tbl_QLDTTXA_HocVienHocPhi_MonHocClient");
        
         msg = new Message(); 
         //List<ConditionReport> cons = new ArrayList<ConditionReport>();
         
         TblQLDTTXAQuiDinhHocPhi tblQLDTTXAQuiDinhHocPhi = tblQLDTTXAQuiDinhHocPhiService.gettblQLDTTXAQuiDinhHocPhi(new Long(1));
         Double tienHaTang = tblQLDTTXAQuiDinhHocPhi.getTienHaTang();
         
         if(hocKyId != null && !hocKyId.equals(new Long(0))){
        	 hocKy = tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(hocKyId);
         }         
         
         tblQLDTTXAHocViens = new ArrayList<TblQLDTTXAHocVien>();
         noHocViens = new ArrayList<Double>();
         
         cons.clear();         
         // lay danh sach hoc vien dang hoc
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("trangThaiId.id");
             con.setValue1(new Long(1));
             con.setOperator("eq");
             cons.add(con);	
         }
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("ten");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hoTen");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 
         List<TblQLDTTXAHocVien> hocViens = tblQLDTTXAHocVienService.findtblQLDTTXAHocVienByCondition(cons, 0, 0);
         
         cons.clear();         
         // lay thong tin tam dung cua cac hoc vien dang hoc
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.trangThaiId.id");
             con.setValue1(new Long(1));
             con.setOperator("eq");
             cons.add(con);	
         }
         tblQLDTTXAHocVienTamDungHocKys = tblQLDTTXAHocVienTamDungHocKyService.findtblQLDTTXAHocVienTamDungHocKyByCondition(cons, 0, 0);
         
         List<TblQLDTTXAHocVienPhieuDangKyMonHoc> phieuDangKyMonHocs = new ArrayList<TblQLDTTXAHocVienPhieuDangKyMonHoc>();
         if(hocKy != null){
        	 cons.clear();
             if(hocKyId != null && !hocKyId.equals(new Long(0))){ 
                 ConditionReport con = new ConditionReport(); 
                 con.setCol("hocKyId.id");
                 con.setValue1(hocKy.getId());
                 con.setOperator("eq");
                 cons.add(con);	
             } 
             { 
                 ConditionReport con = new ConditionReport(); 
                 con.setCol("hocVienId.ten");
                 con.setValue1("asc");
                 con.setOperator("order");
                 cons.add(con);	
             } 
             { 
                 ConditionReport con = new ConditionReport(); 
                 con.setCol("hocVienId.hoTen");
                 con.setValue1("asc");
                 con.setOperator("order");
                 cons.add(con);	
             } 

             phieuDangKyMonHocs = tblQLDTTXAHocVienPhieuDangKyMonHocService.findtblQLDTTXAHocVienPhieuDangKyMonHocByCondition(cons, 0, 0);
    	 
	         for(TblQLDTTXAHocVien hocVien : hocViens){
	        	 boolean coDKMH = false;
	  			  for(TblQLDTTXAHocVienPhieuDangKyMonHoc hocVienPhieuDangKyMonHoc : phieuDangKyMonHocs){ 
	  				   
	  				  if (hocVienPhieuDangKyMonHoc.getHocVienId().getId().equals(hocVien.getId())){
	  					  coDKMH = true;
	  					  break;
	  				  }    
	  			  }
	  			  
	  			  // neu chua bat dau hoc hoac da tam dung
	  			  TblQLDTTXAHocKyTrongNam hocKyBatDauHoc = getHocKyTrongNamBatDauHoc(hocVien);
	  			  if((hocKyBatDauHoc != null && hocKyBatDauHoc.getId() > hocKy.getId()) || isTamDungHocKy(hocVien, hocKy)){
	  				  continue;
	  			  }
	 			  
	   			  if(!coDKMH){
	   				  // kiem tra no HP
	   				  // lay thong tin no hoc phi cac hoc ky truoc
					  Double hocPhiChuaDong = 0.0;
					   cons.clear();
					   { 
			             ConditionReport con = new ConditionReport(); 
			             con.setCol("hocVienHocPhiId.hocVienId.id");
			             con.setValue1(hocVien.getId());
			             con.setOperator("eq");
			             cons.add(con);	
					   } 
					   { 
			             ConditionReport con = new ConditionReport(); 
			             con.setCol("hocVienHocPhiId.hocKyId.id");
			             con.setValue1(hocKyId);
			             con.setOperator("lt");
			             cons.add(con);	
					   } 
					   { 
			             ConditionReport con = new ConditionReport(); 
			             con.setCol("daDongHP");
			             con.setValue1(false);
			             con.setOperator("eq");
			             cons.add(con);	
					   }
					   List<TblQLDTTXAHocVienHocPhiMonHoc> hocPhiMonHocChuaDongs = tblQLDTTXAHocVienHocPhiMonHocService.findtblQLDTTXAHocVienHocPhiMonHocByCondition(cons, 0, 0);
					   
					   if(hocPhiMonHocChuaDongs != null && hocPhiMonHocChuaDongs.size() > 0){
						   for(TblQLDTTXAHocVienHocPhiMonHoc hocPhiMonHocChuaDong : hocPhiMonHocChuaDongs){
							   hocPhiChuaDong += hocPhiMonHocChuaDong.getThanhTien() + tienHaTang;
						   }
					   }
					   
					   // lay thong tin tai khoan
					   Double taiKhoanHocVien = 0.0;
					   cons.clear();
					   { 
			             ConditionReport con = new ConditionReport(); 
			             con.setCol("hocVienId.id");
			             con.setValue1(hocVien.getId());
			             con.setOperator("eq");
			             cons.add(con);	
			         } 
					   List<TblQLDTTXAHocVienTaiKhoan> taiKhoans = tblQLDTTXAHocVienTaiKhoanService.findtblQLDTTXAHocVienTaiKhoanByCondition(cons, 0, 0);
					   if(taiKhoans != null && taiKhoans.size() > 0){
						   taiKhoanHocVien = taiKhoans.get(0).getSoDuTaiKhoan();
					   }
					   
					   Double no = hocPhiChuaDong - taiKhoanHocVien;
					   if(no > 0){
						   tblQLDTTXAHocViens.add(hocVien);
						   noHocViens.add(no);
					   }
	   			  }
	          }
         }
         
         
         
		  
		  return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Report tblQLDTTXAHocVienXetTamDung Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 }


public void setMsg(Message msg) {
	this.msg = msg;
} 

	public String update() throws Exception{
		try{ 	
		      Map session = ActionContext.getContext().getSession();
		      taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
		      if( taiKhoanDangNhap == null) return "noprivilege";
		     ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
		     tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
		     if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien", "sua")){ 
		         return "noprivilege"; 
		     } 
		     tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient");
		        
		     tblQLDTTXAHocVienTamDungHocKyService = (TblQLDTTXAHocVienTamDungHocKyService) context.getBean("tbl_QLDTTXA_HocVien_TamDung_HocKyClient");
		        
		     tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient");
		     	
		     TblQLDTTXAHocKyTrongNam hkTamDung = tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(new Long(hocKyTamDungId));
		     JSONArray jsonArr = (JSONArray) JSONSerializer.toJSON( dataList );
		 	for(int i = 0; i < jsonArr.size() ; i++){
		 		JSONObject obj = jsonArr.getJSONObject(i);
		 		int hvId = obj.getInt("hocVienId");
		 		String ghiChu = obj.getString("ghiChu");
		 		
		 		TblQLDTTXAHocVienTamDungHocKy tamDungHocKy = new TblQLDTTXAHocVienTamDungHocKy();
		 		tamDungHocKy.setHocKyId(hkTamDung);
		 		tamDungHocKy.setHocVienId(tblQLDTTXAHocVienService.gettblQLDTTXAHocVien(new Long(hvId)));
		 		tamDungHocKy.setQdPDT(0);
		 		tamDungHocKy.setLyDo(ghiChu);
		 		tblQLDTTXAHocVienTamDungHocKyService.createtblQLDTTXAHocVienTamDungHocKy(tamDungHocKy);
		 	}
		     
		     return Action.SUCCESS; 
	      } 
	      catch(Exception e){ 
	         System.out.println("Loi Update tblQLDTTXABuocThoiHoc Action"); 
	         e.printStackTrace(); 
	         return Action.ERROR; 
	      } 
	}

	private String dataList;
	public String getDataList() {
		return dataList;
	}
	public void setDataList(String dataList) {
		this.dataList = dataList;
	}
	
	private int hocKyTamDungId;
	public int getHocKyTamDungId() {
		return hocKyTamDungId;
	}
	public void setHocKyTamDungId(int hocKyTamDungId) {
		this.hocKyTamDungId = hocKyTamDungId;
	}

 } 
 